<script setup lang='ts'>
import { doToDoFenMiao, duToDoFen, duFenMiaoToDu, duFenToDo } from "./base/duTransform"
import { ref, watch } from 'vue';
const props = withDefaults(defineProps<{
    positionValue?: [number, number, number],
    lonLatFormat: string,
    readonly?: boolean,
    defaultValue?: any,
}>(), { positionValue: () => [0, 0, 0], lonLatFormat: '' })//lonLatFormat: 'SEXAGESIMAL_DEGREE' ||'DEGREES_DECIMAL_MINUTES'
type defaultValueType = [number, number, number]

const emits = defineEmits([
    "update:positionValue"
]);
//度
const longitude = ref()//经度
const latitude = ref()//纬度
const height = ref()
//度分秒
const duFenMiaoLongitud = ref<[number, number, number]>([0, 0, 0])//经度
const duFenMiaoLatitude = ref<[number, number, number]>([0, 0, 0])//纬度
//度分
const duFenLongitude = ref<[number, number]>([0, 0])//经度
const duFenLatitude = ref<[number, number]>([0, 0])//纬度
const mouseFlag = ref(false)
watch(() => props.positionValue, (val) => {
    mouseFlag.value = false
    if (props.lonLatFormat === 'SEXAGESIMAL_DEGREE') {
        duFenMiaoLongitud.value = doToDoFenMiao(val[0])
        duFenMiaoLatitude.value = doToDoFenMiao(val[1])
    } else if (props.lonLatFormat === 'DEGREES_DECIMAL_MINUTES') {
        duFenLongitude.value = duToDoFen(val[0])
        duFenLatitude.value = duToDoFen(val[1])
    } else {
        longitude.value = val[0].toFixed(7)
        latitude.value = val[1].toFixed(7)
    }
    height.value = Number(val[2].toFixed(3))

}, { immediate: true })
//度
const update1 = (e: any, numberValues: defaultValueType | undefined, num: number) => {
    if (numberValues === undefined) return
    let numberArr = [...numberValues]
    let targetValue
    targetValue = (e.target as HTMLInputElement).value as any;
    let min
    let max
    if (num === 0) {
        min = -180
        max = 180
    } else if (num === 1) {
        min = -90
        max = 90
    }
    if (min !== undefined) {
        if (targetValue < min) {
            targetValue = min
        }
    }
    if (max !== undefined) {
        if (targetValue > max) {
            targetValue = max
        }
    }
    numberArr[num] = Number(targetValue)
    emits("update:positionValue", numberArr)
}
//度分秒
const update2 = (numberValues: defaultValueType | undefined, num: number) => {
    if (numberValues === undefined) return
    let numberArr = [...numberValues]
    if (num === 0) {
        const du = duFenMiaoToDu(duFenMiaoLongitud.value)
        numberArr[num] = du
    } else if (num === 1) {
        const du = duFenMiaoToDu(duFenMiaoLatitude.value)
        numberArr[num] = du
    }
    emits("update:positionValue", numberArr)
}
//度分
const update3 = (numberValues: defaultValueType | undefined, num: number) => {
    if (numberValues === undefined) return
    let numberArr = [...numberValues]
    if (num === 0) {
        const du = duFenToDo(duFenLongitude.value)
        numberArr[num] = du
    } else if (num === 1) {
        const du = duFenToDo(duFenLatitude.value)
        numberArr[num] = du
    }
    emits("update:positionValue", numberArr)
}

const resetDefult = () => {
    if (props.defaultValue) {
        emits('update:positionValue', props.defaultValue)
    }
}
</script>

<template>
    <div class="position">
        <div class="position_name">{{ '经纬高度' }}</div>
        <div class="position_type" v-if="lonLatFormat === 'SEXAGESIMAL_DEGREE'">
            <div class="input2" style="margin-bottom: 5px;">
                <div class="input2_index">
                    <div class="input2_index_div"><input type="number" class="input_number"
                            v-model="duFenMiaoLongitud[0]" @blur="update2(positionValue, 0)"
                            @keydown.enter="update2(positionValue, 0)" placeholder="请输入" />
                        <span>{{ '°' }}</span>
                    </div>
                    <div class="input2_index_div"><input type="number" class="input_number"
                            v-model="duFenMiaoLongitud[1]" @blur="update2(positionValue, 0)"
                            @keydown.enter="update2(positionValue, 0)" placeholder="请输入" />
                        <span>{{ "'" }}</span>
                    </div>
                    <div class="input2_index_div"><input type="number" class="input_number"
                            v-model="duFenMiaoLongitud[2]" @blur="update2(positionValue, 0)"
                            @keydown.enter="update2(positionValue, 0)" placeholder="请输入" />
                        <span>{{ '"' }}</span>
                    </div>
                </div>
            </div>
            <div class="input2" style="margin-bottom: 5px;">
                <div class="input2_index">
                    <div class="input2_index_div"> <input type="number" class="input_number"
                            v-model="duFenMiaoLatitude[0]" @blur="update2(positionValue, 1)"
                            @keydown.enter="update2(positionValue, 1)" placeholder="请输入" />
                        <span>{{ '°' }}</span>
                    </div>

                    <div class="input2_index_div"><input type="number" class="input_number"
                            v-model="duFenMiaoLatitude[1]" @blur="update2(positionValue, 1)"
                            @keydown.enter="update2(positionValue, 1)" placeholder="请输入" />
                        <span>{{ "'" }}</span>
                    </div>

                    <div class="input2_index_div"> <input type="number" class="input_number"
                            v-model="duFenMiaoLatitude[2]" @blur="update2(positionValue, 1)"
                            @keydown.enter="update2(positionValue, 1)" placeholder="请输入" />
                        <span>{{ '"' }}</span>
                    </div>
                </div>
            </div>
            <div class="input2">
                <div class="input2_index">
                    <div class="input2_index_div"> <input type="number" class="input_number" :value="height"
                            @blur="update1($event, positionValue, 2)" @keydown.enter="update1($event, positionValue, 2)"
                            step="0.001" placeholder="请输入高度" />
                        <span>{{ 'm' }}</span>
                    </div>
                </div>
            </div>
        </div>
        <div class="position_type" v-else-if="lonLatFormat === 'DEGREES_DECIMAL_MINUTES'">
            <div class="input2" style="margin-bottom: 5px;">
                <div class="input2_index">
                    <div class="input2_index_div"> <input type="number" class="input_number" v-model="duFenLongitude[0]"
                            @blur="update3(positionValue, 0)" @keydown.enter="update3(positionValue, 0)"
                            placeholder="请输入" /><span>{{ '°' }}</span></div>

                    <div class="input2_index_div"> <input type="number" class="input_number" v-model="duFenLongitude[1]"
                            @blur="update3(positionValue, 0)" @keydown.enter="update3(positionValue, 0)"
                            placeholder="请输入" /><span>{{ "'" }}</span></div>
                </div>
            </div>
            <div class="input2" style="margin-bottom: 5px;">
                <div class="input2_index">
                    <div class="input2_index_div"> <input type="number" class="input_number" v-model="duFenLatitude[0]"
                            @blur="update3(positionValue, 1)" @keydown.enter="update3(positionValue, 1)"
                            placeholder="请输入" /><span>{{ '°' }}</span></div>

                    <div class="input2_index_div"> <input type="number" class="input_number" v-model="duFenLatitude[1]"
                            @blur="update3(positionValue, 1)" @keydown.enter="update3(positionValue, 1)"
                            placeholder="请输入" /><span>{{ "'" }}</span></div>

                </div>
            </div>
            <div class="input2">
                <div class="input2_index">
                    <div class="input2_index_div"><input type="number" class="input_number" :value="height"
                            @blur="update1($event, positionValue, 2)" @keydown.enter="update1($event, positionValue, 2)"
                            step="0.001" placeholder="请输入高度" /><span>m</span></div>
                </div>

            </div>
        </div>
        <div class="position_type" v-else>
            <div class="input2" style="margin-bottom: 5px;">
                <div class="input2_index">
                    <div class="input2_index_div"><input type="number" class="input_number" :value="longitude"
                            @blur="update1($event, positionValue, 0)" @keydown.enter="update1($event, positionValue, 0)"
                            step="0.0000001" placeholder="请输入经度" /><span>°</span></div>

                </div>
            </div>
            <div class="input2" style="margin-bottom: 5px;">
                <div class="input2_index">
                    <div class="input2_index_div"><input type="number" class="input_number" :value="latitude"
                            @blur="update1($event, positionValue, 1)" @keydown.enter="update1($event, positionValue, 1)"
                            step="0.0000001" placeholder="请输入纬度" /><span>°</span></div>
                </div>
            </div>
            <div class="input2">
                <div class="input2_index">
                    <div class="input2_index_div">
                        <input type="number" class="input_number" :value="height"
                            @blur="update1($event, positionValue, 2)" @keydown.enter="update1($event, positionValue, 2)"
                            step="0.001" placeholder="请输入高度" />
                        <span>m</span>
                    </div>
                </div>

            </div>
        </div>
        <div class="checkbox1">
            <div v-if="!readonly && defaultValue !== undefined && ((positionValue[0] !== defaultValue[0]) || (positionValue[1] !== defaultValue[1]) || (positionValue[2] !== defaultValue[2]))"
                class="checkbox" @click.stop.prevent="resetDefult" @mouseover="mouseFlag = true"
                @mouseout="mouseFlag = false">
                <es-icon :name="'huaban'" :color="mouseFlag ? 'rgba(230, 230, 230, 1)' : 'rgba(230, 230, 230, 0.4)'"
                    :size="14" />
            </div>
        </div>

    </div>
</template>

<style scoped>
.position {
    width: 100%;
    border-top: 1px solid #111111;
    border-bottom: 1px solid #111111;
    padding-top: 10px;
    padding-bottom: 10px;
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
}

.position_name {
    width: 70px;
    line-height: 26px;
}

.position_type {
    flex: 1;
    display: flex;
    flex-direction: column;
    /* border: 2px solid rgba(127, 127, 127, 0.219); */
    box-sizing: border-box;
    padding: 2px;
}

.position_type>.input2 {
    width: 100%;
    display: flex;
}

.input2>.input2_index {
    /* width: 100%; */
    flex: 1;
    height: 26px;
    display: flex;
}

.input2>.input2_index>.input2_index_div {
    flex: 1;
    position: relative;
}

.input2>.input2_index>.input2_index_div>input {
    width: 100%;
    height: 26px;
    background: rgba(28, 28, 29, 0.6);
    border-radius: 4px;
    border: 1px solid #3b3c40;
    margin-right: 0;
    color: rgba(230, 230, 230, 1);
    padding: 0 13px 0 11px;
    box-sizing: border-box;
}

.input2>.input2_index>.input2_index_div>span {
    position: absolute;
    right: 5px;
    top: 0;
    display: inline-block;
    /* width: 12px; */
    height: 26px;
    line-height: 26px;
    text-align: center;
    color: rgba(255, 255, 255, 0.5);
}

.input2>.input2_index>span {
    display: inline-block;
    width: 8px
}

.checkbox1 {
    width: 20px;
    height: 20px;
    box-sizing: border-box;
    margin-left: 10px;
    margin-top: 5px;
}

.checkbox {
    width: 20px;
    height: 20px;
    cursor: pointer;
    border: 1px solid rgba(230, 230, 230, 0.4);
    box-sizing: border-box;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 4px;
}

.input2>.input2_index>.input2_index_div>input:hover {
    border: 1px solid rgba(44, 104, 247, 1);
}

.input2>.input2_index>.input2_index_div>input:focus {
    border: 1px solid #2c68f7;
}

.input_number::-webkit-outer-spin-button,
.input_number::-webkit-inner-spin-button {
    -webkit-appearance: none;
}

.input_number[type="number"] {
    -moz-appearance: textfield;
}
</style>
